package crew.gwt.client.widgets.util;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import crew.gwt.client.domain.PersistableEntity;
import crew.gwt.client.widgets.ClassicLabel;
import crew.gwt.client.widgets.FormPopupPanel;
import crew.gwt.client.widgets.handler.DialogHandler;


public class EntityGridFeatureManager<T extends PersistableEntity<T>> extends EntityGridFeature<Label,T> {
	
	private final Map<EntityGridFeature<?,T>,CheckBox> listOfFeatures = new HashMap<EntityGridFeature<?,T>,CheckBox>();
	private final RichEntityGrid<T> grid;
	
	public EntityGridFeatureManager(RichEntityGrid<T> grid) {
		//Keep grid reference
		this.grid = grid;
		
		//Title
		setTitle("Ajouter une fonctionnalité");

		//Header
		final Button b = new Button("<img src='img/Symbol-Add.png' />",new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				showSettings();
			}
		});
		b.setTitle("Ajouter une fonctionnalité à cette table");
		setHeader(b);
		
	}
	
	protected void showSettings() {
		final VerticalPanel settingsPl = new VerticalPanel();
		settingsPl.setSpacing(5);

		settingsPl.add(new ClassicLabel("Liste des fonctionnalités :"));
		for(CheckBox ckFeature : listOfFeatures.values()){
			settingsPl.add(ckFeature);
		}
		
		new FormPopupPanel(settingsPl, new DialogHandler() {
			@Override
			public void onConfirmClick() {
				onSettingsChange();
			}
			@Override
			public void onCancelClick() {
			}
		});
	}

	public void addFeature(EntityGridFeature<?,T> feature){
		CheckBox ck = new CheckBox(feature.getTitle());
		ck.addStyleName("ClassicLabel");
		listOfFeatures.put(feature,ck );
	}

	protected void onSettingsChange() {
		for(EntityGridFeature<?,T> feature : listOfFeatures.keySet()){
			final Boolean toActivate = ((CheckBox)listOfFeatures.get(feature)).getValue();
			if( toActivate && ! grid.getFeatureOnColumn().containsKey(feature) ){
				grid.addEntityGridFeature(feature);
			} else if ( ! toActivate && grid.getFeatureOnColumn().containsKey(feature) ){
				grid.removeEntityGridFeature(feature);
			}
		}
	}

//	@Override
//	protected UpdatableEntityWidget<Label,T> getEnityRowWidget(T entityOfWidget) {
//		UpdatableEntityWidget<Label,T> widget = new UpdatableEntityWidget<Label,T>(new Label("-"),entityOfWidget) {
//			@Override
//			public void onContextChange() {
//				// Construct the widget
//			}
//		};
//		getWidgetOfEntity().put(entityOfWidget, widget);
//		return widget;
//	}

	@Override
	protected UpdatableEntityWidget<Label,T> createUpdatableEntityWidget(T entityOfWidget) {
		final UpdatableEntityWidget<Label,T> widget = new UpdatableEntityWidget<Label,T>(new Label("-"),entityOfWidget) {
			@Override
			public void onContextChange() {
				// Construct the widget
			}
		};
		return widget;
	}

}
